package com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.sub

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.bean.XuRuJiLvBean
import com.example.wallet.db.UserInfoManager
import com.example.wallet.global.AppGlobals
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 蓄入记录
 * */
class XuRuJiLvFragment : SimpleListFragment<XuRuJiLvBean>() {

    companion object {
        fun newInstance(): XuRuJiLvFragment {
            val args = Bundle()
            val fragment = XuRuJiLvFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private val ssl = AppGlobals.getApplication().getString(R.string.wallet_sst)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
//        isEnableLoadMore=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        mHeader.setBackgroundResource(R.color.bg_color_0)
        addHeader(R.layout.xu_ru_ji_lv_header_fragment)
        val mToolbar:Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setNavigationOnClickListener {
            onBackPressedSupport()
        }
    }

    override fun getItemLayout() = R.layout.xu_ru_ji_lv_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: XuRuJiLvBean?) {
        helper?.setText(R.id.valueTv,  "${item?.txNums} $ssl")
            ?.setText(R.id.typeTv, item?.account)
            ?.setText(R.id.timeTv, item?.createTime)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(XuRuJiLvBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val map = mapOf(
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryWelfareLog)
            .success {
                parseDate(it)
            }.error { _, _ -> onFailed() }
            .failure { onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"] ?: ""
        val array = JSON.parseArray(JSON.toJSONString(list), XuRuJiLvBean::class.java)
        loadListDate(array)
    }
}